<style>
	body{
		background-color: #ccffcc;
	}
</style>


<div >
<h1>公告通知列表页面</h1>

	<!-- 全选暂时不考虑 跨页面全选 -->
<table border=1>
	<form id="searchForm">
	<tr>
		<td><button type="button" onclick="add()">发布公告</button></td>
		<td ><button type="button" onclick="delMore()">批量删除</button></td>
		<td><button type="button" id="exportNotice" >导出公告</button></td>
		<!--<td><button onclick="importNotice()">导入公告</button></td>-->
		<td>公告标题：<input  id="title" name="title" #if($notice.title) value="${notice.title}" #end  /></td>
		<td>开始时间：<input id="startdate" name="startdate" #if($notice.startdate) value="${notice.startdate}" #end /></td>
		<td>结束时间：<input  id="enddate" name="enddate" #if($notice.enddate) value="${notice.enddate}" #end /></td>
		<td>发布范围：
			<select name="level" id="level">
				<option value="">全部</option>
				<option value="0" #if($notice.level==0) selected="selected" #end >所有人</option>
				<option value="1" #if($notice.level==1) selected="selected" #end >已注册人员</option>
				<option value="2" #if($notice.level==2) selected="selected" #end >单位管理员</option>
				<option value="3" #if($notice.level==3) selected="selected" #end >专家</option>
			</select>

		</td>
		<td>状态：
			<select name="state" id="state">
				<option  value="">全部</option>
				<option value="1" #if($notice.state==1) selected="selected" #end >已发布</option>
				<option value="0" #if($notice.state==0) selected="selected" #end >未发布</option>
			</select>
		</td>
		<td>类型：
			<select name="type" id="type">
				<option value="">全部</option>
				<option value="0" #if($notice.type==0) selected="selected" #end >通知</option>
				<option value="1" #if($notice.type==1) selected="selected" #end >公告</option>
			</select>
		</td>

		<td colspan="3">
			&nbsp;&nbsp;&nbsp;
			<button type="button" onclick="searchNotice()">查询</button>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			<button type="button" onclick="reSetFrom()">重置</button>
		</td>
	</tr>

	<tr>
		<td  colspan="11">
		页数：<input id="page" name="page" placeholder="1" value="${page}" />
		条数：
		<select name="row" id="row">
			<option value="2" #if($row == 1 ) selected="selected" #end >2</option>
			<option value="5" #if($row == 5 ) selected="selected" #end >5</option>
			<option value="10" #if($row == 10 ) selected="selected" #end>10</option>
		</select>
		<button onclick="testpage()">跳转指定页面</button>
		</td>
	</tr>
	</form>

	<tr>
		<td align="center"><input id="selectall" type="checkbox" /></td>
		<td>序号</td>
		<td>标题</td>
		<!--<td>内容</td>-->
		<td>分类</td>
		<td>公开范围</td>
		<!--<td>附件链接地址</td>-->
		 <td>状态</td>
		<td>开始日期</td>
		<td>结束日期</td>
		<td>创建人姓名</td>
		<td>创建时间</td>

		<td>操作</td>
	</tr>
	#if( $noticeList )
		#set ($rowNum=0)
		#foreach($res in $noticeList)
		#set($rowNum=$rowNum+1)
			<tr>
				<td align="center"><input id="${res.id}" name="selectsingle" style="width:20px" type="checkbox" /></td>
				<td>${rowNum}</td>
				<td><a style="color: blue;cursor: pointer;" onclick="goNotice('${res.id}')">${res.title}</a></td>
				<!--<td>
				  #if($res.content.length()>10)
						#set($res.content = $res.content.substring(0,10))
					   ${res.content}
				   #else
					   ${res.content}
				   #end
				</td>-->
				<td>
					#if($res.type == 0)
						<span>通知</span>
					#elseif($res.type == 1)
						<span>公告</span>
					#else
						<span>未知类型</span>
					#end
				</td>

				<td>
					#if($res.level == 0)
						<span>所有人</span>
					#elseif($res.level == 1)
						<span>系统已注册人员</span>
					#elseif($res.level == 2)
						<span>单位管理员</span>
					#elseif($res.level == 3)
						<span>专家</span>
					#else
						<span>未知类型</span>
					#end
				</td>

				<!--<td>${res.linkurl}</td>-->

				<td>
					#if($res.state == 1)
						<span>已发布</span>
					#else
						<span>未发布</span>
					#end
				</td>

				<td>${res.startdate}</td>
				<td>${res.enddate}</td>
				<!-- <td>${res.createuserid}</td> -->
				<td>${res.createusername}</td>
				<td>${res.createtime}</td>


				<td>
					<a style="color: blue;cursor: pointer;" onclick="update('${res.id}')">修改</a>
					/
					<a style="color: blue;cursor: pointer;" onclick="del('${res.id}')">删除</a>
				</td>
			</tr>
		#end
	#else
		<div></div>
	#end
		

	
</table>
</div>

<script type="text/javascript">

	function add(){//跳转到 添加页面
		location.href = "${path}/notice/jump?optType=add";
	}
	
	function update(id){//跳转到 修改页面
		location.href = "${path}/notice/jump?optType=update&id="+id;
	}
    function delMore(){
	    var id  = "";
        var children = document.getElementsByName("selectsingle");
        if(children){
			for(var i = 0; i<children.length ;i++){
				if(children[i].checked){//选中了,
                    id = id + children[i].getAttribute("id") +",";
				}
			}
		}
		if(id==""){
            alert("请选择要删除的数据");
            return;
		}
        id = id.substring(0,(id.length-1));
        del(id);
	}

	function del(id){//删除操作
		if(!confirm("确定要删除吗?")) return false;

		var url = "${path}/notice/delete?id="+id;
		 $.ajax({
				url:url,
				Type:"POST",
				data:{},
				dataType:"json",
				success:function(data){
					console.log(data.message);
					alert("删除成功！");
					location.href= "${path}/notice/index";
				},
				error:function(data){
					console.log("保存失败！");
					console.log(data.message);
					alert("保存失败！");
				}
			});
		
	}
	
	function goNotice(id){//跳转到 查看yemian
		console.log(id);
		location.href = "${path}/notice/jump?optType=detail&id="+id;
		/* $.ajax({
			url:"${path}/notice/goNotice";
			data:{"id",id},
			dataType:"json",
		}) */
	}
	
	
	function searchNotice(){
		//var serchVal = $("#searchVal").val();
		var data = $("#searchForm").serialize();
		location.href = "${path}/notice/index?"+data;
	}
	
	function testpage(){
		var page = $("#page").val();
		var row = $("#row").val();
		location.href = "${path}/notice/index?row="+row+"&page="+page;
	}


    function reSetFrom(){
		var hrefsrc = location.href;
        hrefsrc = hrefsrc.substring(0,hrefsrc.indexOf("?"));
        location.href = hrefsrc;
	}

	$("#exportNotice").on("click",function () {
        if(confirm("是否确定要导出数据吗？")){
            //防止多次点击
            $("#exportNotice").attr("disabled","disabled");
            location.href = "${path}/notice/export";
        }
    });



    function importNotice(){

    }

    //实现全选和取消全选
    $("#selectall").on("click",function () {
        //jquery 方式 有时候 会不起左右，这里用JavaScript方式
		//var isSelected = $("#selectall").is(':checked');
        var children = document.getElementsByName("selectsingle");
        if(children){
            for(var i=0;i<children.length;i++){
                if(document.getElementById("selectall").checked){//全选
                    children[i].checked=true;
				}else{
                    children[i].checked=false;
				}
            }
        }

    });


	//单个点击的时候，实现全选和取消全选
    $("[name='selectsingle']").on("click",function () {
		if(this.checked){//选中了
			//判断其他项是不是 也选中了，
			if(isCheckAll('selectsingle')){//加上这个刚好全选
                document.getElementById("selectall").checked=true;
			}
		}else{//该项点击的是 取消选中，所以不管什么 selectall 都为不全选
            document.getElementById("selectall").checked=false;
		}
    });
	//判断所有的 checkbox是否全选
    function isCheckAll(name) {
        var children = document.getElementsByName(name);
        var flag = true;
        if(children){
            for(var i=0;i<children.length;i++){
                if(!children[i].checked){//有 没选中的
                    flag = false;
				}
			}
		}
        return flag;
    }
</script>